package com.zxmc.server.service.impl;

import com.zxmc.common.constant.MessageConstant;
import com.zxmc.common.context.BaseContext;
import com.zxmc.common.exception.AccountNotFoundException;
import com.zxmc.common.exception.PasswordErrorException;
import com.zxmc.common.exception.RegisterErrorException;
import com.zxmc.common.exception.UserUpdateException;
import com.zxmc.common.utils.RegularUtil;
import com.zxmc.pojo.dto.UserLoginDto;
import com.zxmc.pojo.entity.Admin;
import com.zxmc.pojo.entity.User;
import com.zxmc.server.mapper.AdminMapper;
import com.zxmc.server.mapper.UserMapper;
import com.zxmc.server.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;

import static com.zxmc.common.constant.AvatarConstant.AVATAR_DEFAULT;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void register(UserLoginDto userLoginDto) {
        if(!RegularUtil.checkName(userLoginDto.getUsername()) || !RegularUtil.checkPwd(userLoginDto.getPassword())) {
            //注册失败
            throw new RegisterErrorException(MessageConstant.REGISTER_FAILED);
        }
        Admin admin = new Admin();
        BeanUtils.copyProperties(userLoginDto, admin);
//        user.setCreatedAt(LocalDateTime.now());
        admin.setPassword(DigestUtils.md5DigestAsHex(userLoginDto.getPassword().getBytes()));

        adminMapper.insert(admin);
    }

    @Override
    public Admin login(UserLoginDto userLoginDto) {
        String username = userLoginDto.getUsername();
        String password = userLoginDto.getPassword();

        //1、根据用户名查询数据库中的数据
        Admin admin = adminMapper.list(userLoginDto);

        //2、处理各种异常情况（用户名不存在、密码不对）
        if (admin == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // 对前端传过来的明文密码进行md5加密处理

        //TODO
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(admin.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }



        //3、返回实体对象
        return admin;
    }

    @Override
    public Admin list() {
        Integer empId = BaseContext.getCurrentId();
        return adminMapper.listById(empId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Admin admin) {
        Admin admin2 = list();
        admin.setId(admin2.getId());
//        user.setPassword(user2.getPassword());
        if(admin.getPassword()!=null) {
            admin.setPassword(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()));
        }
        if(admin2.equals(admin)) {
            throw new UserUpdateException(MessageConstant.USER_UPDATE_SAME);
        }
        Admin admin1 = listByName(admin.getUsername());
        if(admin1!=null&&!admin1.getUsername().equals(admin.getUsername())) {
            throw new UserUpdateException(MessageConstant.USER_UPDATE_EXIST);
        }
//        if(user.getAvatar()!=null) {
//            user.setAvatar(user2.getAvatar());
//        }
        adminMapper.update(admin);

    }

    private Admin listByName(String username) {
        UserLoginDto userLoginDto = new UserLoginDto();
        userLoginDto.setUsername(username);
        return adminMapper.list(userLoginDto);
    }
}
